<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Functional Java</title>
    <link href="../../style.css" type="text/css" rel="stylesheet">
    <link href="../../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css"
          rel="stylesheet" type="text/css">
    <meta http-equiv="Content-Language" content="en">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta content="Tony Morris" name="author">
    <meta content="Copyright Tony Morris 2008-2010" name="copyright">
    <meta content="Functional,Java,Programming" name="keywords">
    <meta content="Functional Java" name="description">
    <script type="text/javascript">
        var home = new Image();
        home.src = "../../images/buttons/home/1/over.png";
        var examples = new Image();
        examples.src = "../../images/buttons/examples/1/over.png";
        var download = new Image();
        download.src = "../../images/buttons/download/1/over.png";
        var api = new Image();
        api.src = "../../images/buttons/api/1/over.png";
        var source = new Image();
        source.src = "../../images/buttons/source/1/over.png";
        var community = new Image();
        community.src = "../../images/buttons/community/1/over.png";
        var issues = new Image();
        issues.src = "../../images/buttons/issues/1/over.png";
    </script>
    <script type="text/javascript">

        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    </script>
    <script type="text/javascript">

        var pageTracker = _gat._getTracker("UA-4485773-1");
        pageTracker._initData();
        pageTracker._trackPageview();

    </script>
</head>
<body>
<div class="logo">
    <img src="../../images/logo-600x144.png" alt="Functional Java">
</div>
<hr>
<div class="menu">
    <p>
        <a href="../../">
            <img src="../../images/buttons/home/1/up.png" alt="Home" onmouseout="src='../../images/buttons/home/1/up.png'"
                 onmouseover="src='../../images/buttons/home/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../../examples/">
            <img src="../../images/buttons/examples/1/up.png" alt="Examples"
                 onmouseout="src='../../images/buttons/examples/1/up.png'"
                 onmouseover="src='../../images/buttons/examples/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../../download">
            <img src="../../images/buttons/download/1/up.png" alt="Download"
                 onmouseout="src='../../images/buttons/download/1/up.png'"
                 onmouseover="src='../../images/buttons/download/1/over.png'">
        </a>
    </p>

    <p>
        <a href="http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/index.html">
            <img src="../../images/buttons/api/1/up.png" alt="API" onmouseout="src='../../images/buttons/api/1/up.png'"
                 onmouseover="src='../../images/buttons/api/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../../source/">
            <img src="../../images/buttons/source/1/up.png" alt="Source" onmouseout="src='../../images/buttons/source/1/up.png'"
                 onmouseover="src='../../images/buttons/source/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../../community/">
            <img src="../../images/buttons/community/1/up.png" alt="Community"
                 onmouseout="src='../../images/buttons/community/1/up.png'"
                 onmouseover="src='../../images/buttons/community/1/over.png'">
        </a>
    </p>

    <p>
        <a href="http://code.google.com/p/functionaljava/issues/list">
            <img src="../../images/buttons/issues/1/up.png" alt="Issues" onmouseout="src='../../images/buttons/issues/1/up.png'"
                 onmouseover="src='../../images/buttons/issues/1/over.png'">
        </a>
    </p>
</div>
<div class="main">
<div>
<h4>Examples 1.5</h4>
<h6><em><a href="../../examples">Examples (BGGA)</a></em></h6>

<p>
    These examples use standard Java 1.5 syntax where an instance of the <code>fj.F</code> interface is
    passed to emulate closures.
</p>
<a name="Examples.list"></a>

<div>
<ul>
    <li><a href="#Array.exists">Array exists</a></li>
    <li><a href="#Array.filter">Array filter</a></li>
    <li><a href="#Array.foldLeft">Array foldLeft</a></li>
    <li><a href="#Array.forall">Array forall</a></li>
    <li><a href="#Array.map">Array map</a></li>
    <li><a href="#List.map">List map</a></li>
    <li><a href="#List.sort">List sort</a></li>
    <li><a href="#Option.bind">Option bind</a></li>
    <li><a href="#Option.filter">Option filter</a></li>
    <li><a href="#Option.map">Option map</a></li>
    <li><a href="#HList.append">HList append</a></li>
    <li><a href="#HList.foldRight">HList foldRight</a></li>
</ul>
<hr>
<a name="Array.exists"></a><h5>Array exists <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Array_exists.java">&#x00A7;</a></h5>

<p>Checks for the existence of a String that has all lower case characters. In this case it is true (since, the case of
    &quot;what&quot;), but without this specific case, it is false. This example uses List.forall to test the characters
    of the String.</p><pre class="java:nocontrols" name="code" id="Array.exists.example">import fj.F;
import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static fj.function.Characters.isLowerCase;

public final class Array_exists {
  public static void main(final String[] args) {
    final Array&lt;String&gt; a = array(&quot;Hello&quot;, &quot;There&quot;, &quot;what&quot;, &quot;DAY&quot;, &quot;iS&quot;, &quot;iT&quot;);
    final boolean b = a.exists(new F&lt;String, Boolean&gt;() {
      public Boolean f(final String s) {
        return fromString(s).forall(isLowerCase);
      }
    });
    System.out.println(b); // true (&quot;what&quot; provides the only example; try removing it)
  }
}
</pre>
<hr>
<a name="Array.filter"></a><h5>Array filter <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Array_filter.java">&#x00A7;</a></h5>

<p>Removes elements from an array that do not meet a certain criteria. In this case, we are using an array of integers
    and the filter removes any odd numbers.</p><pre class="java:nocontrols" name="code" id="Array.filter.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;
import static fj.function.Integers.even;

public final class Array_filter {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final Array&lt;Integer&gt; b = a.filter(even);
    arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
  }
}
</pre>
<hr>
<a name="Array.foldLeft"></a><h5>Array foldLeft <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Array_foldLeft.java">&#x00A7;</a></h5>

<p>Reduces the list applying a function per element. In this case, the fold sums the elements with a starting value of
    0. Since 0 + 97 + 44 + 67 + 3 + 22 + 90 + 1 + 77 + 98 + 1078 + 6 + 64 + 6 + 79 + 42 == 1774 the result of the fold
    is 1774.</p><pre class="java:nocontrols" name="code" id="Array.foldLeft.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.function.Integers.add;

public final class Array_foldLeft {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final int b = a.foldLeft(add, 0);
    System.out.println(b); // 1774
  }
}
</pre>
<hr>
<a name="Array.forall"></a><h5>Array forall <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Array_forall.java">&#x00A7;</a></h5>

<p>Checks that all Strings in the array have lower case characters. In this case, the check fails because of the case of
    &quot;There&quot;, however, the removal of this case produces a result of true.</p><pre class="java:nocontrols"
                                                                                            name="code"
                                                                                            id="Array.forall.example">import fj.F;
import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static fj.function.Characters.isLowerCase;

public final class Array_forall {
  public static void main(final String[] args) {
    final Array&lt;String&gt; a = array(&quot;hello&quot;, &quot;There&quot;, &quot;what&quot;, &quot;day&quot;, &quot;is&quot;, &quot;it&quot;);
    final boolean b = a.forall(new F&lt;String, Boolean&gt;() {
      public Boolean f(final String s) {
        return fromString(s).forall(isLowerCase);
      }
    });
    System.out.println(b); // false (&quot;There&quot; is a counter-example; try removing it)
  }
}
</pre>
<hr>
<a name="Array.map"></a><h5>Array map <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Array_map.java">&#x00A7;</a></h5>

<p>Maps a function across the array of integers. In this case, the function adds 42 to each element of the array to
    produce a new array.</p><pre class="java:nocontrols" name="code" id="Array.map.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.function.Integers.add;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;

public final class Array_map {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(1, 2, 3);
    final Array&lt;Integer&gt; b = a.map(add.f(42));
    arrayShow(intShow).println(b); // {43,44,45}
  }
}
</pre>
<hr>
<a name="List.map"></a><h5>List map <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/List_map.java">&#x00A7;</a></h5>

<p>Maps a function across a list of integers. This is similar to the Array map. This example adds 42 to each element of
    the list to produce a new list.</p><pre class="java:nocontrols" name="code" id="List.map.example">import fj.data.List;
import static fj.data.List.list;
import static fj.function.Integers.add;
import static fj.pre.Show.intShow;
import static fj.pre.Show.listShow;

public final class List_map {
  public static void main(final String[] args) {
    final List&lt;Integer&gt; a = list(1, 2, 3);
    final List&lt;Integer&gt; b = a.map(add.f(42));
    listShow(intShow).println(b); // [43,44,45]
  }
}
</pre>
<hr>
<a name="List.sort"></a><h5>List sort <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/List_sort.java">&#x00A7;</a></h5>

<p>Sorts the given list by producing a new list using a merge-sort algorithm.</p><pre class="java:nocontrols"
                                                                                      name="code"
                                                                                      id="List.sort.example">import fj.data.List;
import static fj.data.List.list;
import static fj.pre.Ord.intOrd;
import static fj.pre.Show.intShow;
import static fj.pre.Show.listShow;

public final class List_sort {
  public static void main(final String[] args) {
    final List&lt;Integer&gt; a = list(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final List&lt;Integer&gt; b = a.sort(intOrd);
    listShow(intShow).println(b); // [1,3,6,6,22,42,44,64,67,77,79,90,97,98,1078]
  }
}
</pre>
<hr>
<a name="Option.bind"></a><h5>Option bind <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Option_bind.java">&#x00A7;</a></h5>

<p>Binds a function across the optional value type. The function checks if the contained value is even and if it is
    multiples that value by 3 and returns that new value. If the contained value is odd (or if there is no value), then
    no value is returned (none).</p><pre class="java:nocontrols" name="code" id="Option.bind.example">import fj.F;
import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_bind {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = some(8);
    final Option&lt;Integer&gt; o3 = none();
    final Option&lt;Integer&gt; p1 = o1.bind(new F&lt;Integer, Option&lt;Integer&gt;&gt;() {
      public Option&lt;Integer&gt; f(final Integer i) {
        if(i % 2 == 0) return some(i * 3); else return none();
      }
    });
    final Option&lt;Integer&gt; p2 = o2.bind(new F&lt;Integer, Option&lt;Integer&gt;&gt;() {
      public Option&lt;Integer&gt; f(final Integer i) {
        if(i % 2 == 0) return some(i * 3); else return none();
      }
    });
    final Option&lt;Integer&gt; p3 = o3.bind(new F&lt;Integer, Option&lt;Integer&gt;&gt;() {
      public Option&lt;Integer&gt; f(final Integer i) {
        if(i % 2 == 0) return some(i * 3); else return none();
      }
    });
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // Some(24)
    optionShow(intShow).println(p3); // None
  }
}
</pre>
<hr>
<a name="Option.filter"></a><h5>Option filter <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Option_filter.java">&#x00A7;</a></h5>

<p>Removes the value from the optional value if it does not match a given predicate. In this case the condition for
    preservation is that the contained value is an even number.</p><pre class="java:nocontrols" name="code"
                                                                        id="Option.filter.example">import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.function.Integers.even;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_filter {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = none();
    final Option&lt;Integer&gt; o3 = some(8);
    final Option&lt;Integer&gt; p1 = o1.filter(even);
    final Option&lt;Integer&gt; p2 = o2.filter(even);
    final Option&lt;Integer&gt; p3 = o3.filter(even);
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // None
    optionShow(intShow).println(p3); // Some(8)
  }
}
</pre>
<hr>
<a name="Option.map"></a><h5>Option map <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/Option_map.java">&#x00A7;</a></h5>

<p>Maps a function across the optional value type. The function adds 42 to any contained value.</p><pre
        class="java:nocontrols" name="code" id="Option.map.example">import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.function.Integers.add;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_map {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = none();
    final Option&lt;Integer&gt; p1 = o1.map(add.f(42));
    final Option&lt;Integer&gt; p2 = o2.map(add.f(42));
    optionShow(intShow).println(p1); // Some(49)
    optionShow(intShow).println(p2); // None
  }
}
</pre>
<hr>
<a name="HList.append"></a><h5>HList append <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/HList_append.java">&#x00A7;</a></h5>

<p>Appends two heteregeneous lists. The type signature grows proportionally to the size of the list - an unfortunate
    consequence without type inference.</p><pre class="java:nocontrols" name="code" id="HList.append.example">import static fj.data.hlist.HList.HCons;
import static fj.data.hlist.HList.HNil;
import static fj.data.hlist.HList.HAppend.append;
import static fj.data.hlist.HList.HAppend;
import static fj.data.hlist.HList.nil;

/**
 * Append two heterogeneous lists
 */
public class HList_append {
  public static void main(final String[] args) {
    // The two lists
    final HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;&gt; a =
      nil().extend(true).extend(3).extend(&quot;Foo&quot;);
    final HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt; b =
      nil().extend(new Integer[]{1, 2}).extend(&quot;Bar&quot;).extend(4.0);

    // A lot of type annotation
    final HAppend&lt;HNil, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt; zero = append();
    final HAppend&lt;HCons&lt;Boolean, HNil&gt;, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt; one = append(zero);
    final HAppend&lt;HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt; two = append(one);
    final HAppend&lt;HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;&gt;,
      HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt;&gt;
      three = append(two);

    // And all of that lets us append one list to the other.
    final HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt;
      x = three.append(a, b);

    // And we can access the components of the concatenated list in a type-safe manner
    System.out.println(x.head()); // Foo
    System.out.println(x.tail().tail().tail().tail().head()); // Bar
  }
}
</pre>
<hr>
<a name="HList.foldRight"></a><h5>HList foldRight <a
        href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/1.5/HList_foldRight.java">&#x00A7;</a></h5>

<p>Fold across a type-safe heterogeneous list.</p><pre class="java:nocontrols" name="code" id="HList.foldRight.example">import fj.F;
import static fj.Function.identity;
import fj.P2;
import fj.Unit;
import static fj.Unit.unit;
import static java.lang.System.out;

import fj.data.hlist.HList;
import static fj.data.hlist.HList.single;
import static fj.data.hlist.HList.Apply;
import static fj.data.hlist.HList.HFoldr;
import static fj.function.Integers.multiply;
import static fj.function.Integers.add;
import static fj.function.Strings.length;

public class HList_foldRight {

  public static void main(final String[] args) {

    // A heterogeneous list of functions to compose
    final HList.HCons&lt;F&lt;String, Integer&gt;, HList.HCons&lt;F&lt;Integer, Integer&gt;, HList.HCons&lt;F&lt;Integer, Integer&gt;, HList.HNil&gt;&gt;&gt; functions =
      single(add.f(1)).extend(multiply.f(2)).extend(length);

    // A lot of type handwaving to convince Java that this is all going to work
    final Apply&lt;Unit, P2&lt;F&lt;String, Integer&gt;, F&lt;Integer, Integer&gt;&gt;, F&lt;String, Integer&gt;&gt;
      comp1 = Apply.comp();
    final Apply&lt;Unit, P2&lt;F&lt;Integer, Integer&gt;, F&lt;Integer, Integer&gt;&gt;, F&lt;Integer, Integer&gt;&gt;
      comp0 = Apply.comp();
    final HFoldr&lt;Unit, F&lt;Integer, Integer&gt;, HList.HNil, F&lt;Integer, Integer&gt;&gt;
      fold0 = HFoldr.hFoldr();
    final HFoldr&lt;Unit, F&lt;Integer, Integer&gt;, HList.HCons&lt;F&lt;String, Integer&gt;,
          HList.HCons&lt;F&lt;Integer, Integer&gt;, HList.HCons&lt;F&lt;Integer, Integer&gt;, HList.HNil&gt;&gt;&gt;, F&lt;String, Integer&gt;&gt;
      fold2 = HFoldr.hFoldr(comp1, HFoldr.hFoldr(comp0, HFoldr.hFoldr(comp0, fold0)));
    final F&lt;Integer, Integer&gt; id = identity();

    // Compose the list and apply the resulting function to a value.
    // Unit is used because composition has only one possible implementation.
    out.println(fold2.foldRight(unit(), id, functions).f(&quot;abc&quot;)); // 7
  }
}
</pre>
<hr>
<script type="text/javascript"
        src="../../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Scripts/shCore.js"></script>
<script type="text/javascript"
        src="../../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Scripts/shBrushJava.js"></script>
<script type="text/javascript">
    dp.SyntaxHighlighter.ClipboardSwf = 'flash/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
</script>
</div>
</div>
</div>
<div class="copyright">
    <p>
        Copyright &copy; 2010
    </p>
</div>
</body>
</html>
